package com.ttbj.utils;

import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.ttbj.core.util.security.aes.AESUtils;
import com.ttbj.exception.WebException;
import com.ttbj.util.ResultUtil;

public class QRCodeUtil {

	/**
	 * 主要方法:提取Long类型时间戳
	 * 
	 * @param qrCodeUrl
	 * @return
	 * @throws Exception
	 */
	public static Long getDateMill(String qrCodeConent) throws Exception {
		String number = getArray(qrCodeConent)[1];
		if (!isInteger(number)) {
			ResultUtil.throwExcepion(ResultUtil.createFail("messages", 403), "时间参数不正确");
		}
		return Long.valueOf(number);
	}

	/**
	 * 提取加密订单Id
	 * 
	 * @param qrCodeConent
	 * @return
	 * @throws Exception
	 */
	private static String getEncrypt(String qrCodeConent) throws Exception {
		String encrypt = getArray(qrCodeConent)[2];
		if (StringUtils.isBlank(encrypt)) {
			ResultUtil.throwExcepion(ResultUtil.createFail("messages", 405), "加密内容为空");
		}
		return encrypt;
	}

	/**
	 * 解密订单id
	 * 
	 * @param qrCodeConent
	 * @return
	 * @throws WebException
	 */
	public static Long getId(String qrCodeConent) throws WebException {
		String id = null;
		try {
			id = AESUtils.decrypt(getEncrypt(qrCodeConent));
		} catch (Exception e) {
			e.printStackTrace();
			ResultUtil.throwExcepion(ResultUtil.createFail("messages", 405), "加密内容解密失败");
		}
		if (!isInteger(id)) {
			ResultUtil.throwExcepion(ResultUtil.createFail("messages", 405), "加密内容错误原因非数字");
		}
		return Long.valueOf(id);
	}

	/**
	 * qr code has been tampered with
	 * 
	 * @param qrContentDB
	 *            The content of the qr code stored in the database
	 * @param qrConentPage
	 *            The content of the qr code from parameter passing
	 * @throws WebException
	 */
	public static boolean isTamper(String qrContentDB, String qrConentPage) throws Exception {
		return !qrContentDB.equals(qrConentPage);
	}

	/**
	 * Content formats "/mill/AES(ID)" usage"/"break up after arraylength not be
	 * less than3
	 * 
	 * @param qrCodeConent
	 * @throws Exception
	 */
	private static String[] getArray(String qrCodeConent) throws Exception {
		if (!StringUtils.contains(qrCodeConent, "/")) {
			ResultUtil.throwExcepion(ResultUtil.createFail("messages", 403), "内容为空或格式不规范");
		}
		String[] array = qrCodeConent.split("/");
		if (array.length < 3) {
			ResultUtil.throwExcepion(ResultUtil.createFail("messages", 403), "内容不规范");
		}
		return array;

	}

	/**
	 * Using timestamp parameters contrast db
	 * 
	 * . 36H hours overtime
	 * 
	 * @param qrCodeConent  from parameter passing
	 * 
	 */
	public static boolean isOverTime(String qrCodeConent) throws Exception {
		Long start = getDateMill(qrCodeConent);
		Long end = System.currentTimeMillis();
		long millisPerHour = DateUtils.MILLIS_PER_HOUR * 36;
		return end - start >= millisPerHour;
	}

	/**
	 * 检验时间戳是否是Long类型方式篡改
	 * 
	 * @param str
	 * @return
	 */
	private static boolean isInteger(String str) {
		if (StringUtils.isBlank(str)) {
			return false;
		}
		Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
		return pattern.matcher(str).matches();
	}

}
